package test4.menus;

import de.lessvoid.nifty.EndNotify;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.checkbox.CheckboxControl;
import de.lessvoid.nifty.controls.dropdown.controller.DropDownControl;
import de.lessvoid.nifty.controls.dropdown.controller.DropDownControlNotify;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

public class OptionsManager implements ScreenController, DropDownControlNotify {
    private Nifty nifty;
    private Screen screen;
    private DropDownControl pxlDropDown, resDropDown, clrDropDown;

    public void bind(Nifty nifty, Screen screen) {
        this.nifty = nifty;
        this.screen = screen;

        resDropDown = findDropDownControl("res-dropDown");
        if (resDropDown != null) {
          resDropDown.addNotify(this);
          resDropDown.addItem("640 x 480");
          resDropDown.addItem("800 x 600");
          resDropDown.addItem("1024 x 768");
          resDropDown.addItem("1152 x 720");
          resDropDown.addItem("1280 x 800");
          resDropDown.addItem("1344 x 840");
          resDropDown.addItem("1440 x 900");
          resDropDown.setSelectedItem("1024 x 768");
        }

        clrDropDown = findDropDownControl("clr-dropDown");
        if (clrDropDown != null) {
          clrDropDown.addNotify(this);
          clrDropDown.addItem("Low (16 bpp)");
          clrDropDown.addItem("Medium (24 bpp)");
          clrDropDown.addItem("High (32 bpp)");
          clrDropDown.setSelectedItem("Medium (24 bpp)");
        }

        pxlDropDown = findDropDownControl("pxl-dropDown");
        if (pxlDropDown != null) {
          pxlDropDown.addNotify(this);
          pxlDropDown.addItem("Disabled");
          pxlDropDown.addItem("2x");
          pxlDropDown.addItem("4x");
          pxlDropDown.addItem("6x");
          pxlDropDown.addItem("8x");
          pxlDropDown.addItem("16x");
          pxlDropDown.setSelectedItem("Disabled");
        }
    }

    public void interaction(String action) {
        if(action.equals("save")) {
            nifty.createPopup("popupSave");
            nifty.showPopup(screen, "popupSave", null);
        } else
            nifty.fromXml("/src/test4/menus/main.xml", "start");
    }

    public void save() {
        System.out.println("Resolution: " + resDropDown.getSelectedItem());
        System.out.println("Color: " + clrDropDown.getSelectedItem());
        System.out.println("Pixel Format: " + pxlDropDown.getSelectedItem());
        System.out.println("Fullscreen: " + screen.findControl("fscr-checkbox", CheckboxControl.class).isChecked());
    }

    public void popupSave(final String save) {
        nifty.closePopup("popupSave", new EndNotify() {
          public void perform() {
            if ("yes".equals(save)) {
              nifty.setAlternateKey("fade");
              save();
            }
          }
        });
    }

    private DropDownControl findDropDownControl(final String id) {
        DropDownControl dropDown1 = screen.findControl(id, DropDownControl.class);
        return dropDown1;
    }

    public void onStartScreen() {

    }

    public void onEndScreen() {

    }

    public void dropDownSelectionChanged(DropDownControl ddc) {
        
    }
}
